package jmine.tec.hydra.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.domain.senderRegistry.ScriptSenderRegistry;
import jmine.tec.script.persistent.domain.Script;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Serviço de inclusão de um SenderRegistry que envia mensagens via script.
 * 
 * @author lundberg
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class ScriptSenderRegistryService implements ServiceFiller<ScriptSenderRegistry> {

    public static final String IDENTIFICADOR = "Identificador";

    public static final String MNEMONICO = "Mnemônico";

    public static final String SCRIPT = "Script";

    public static final String DATABASE_ID = "Database ID";

    // Injected
    private PackEntityPersistenceHelper helper;

    // Obrigatórios
    private String mnemonico;

    private Script script;

    // Opcional
    private Long databaseID;

    /**
     * Registra um novo sender para arquivo.
     * 
     * @return FileSenderRegistry
     * @throws PackException PackException
     */
    @Execution
    @Output(propertyName = IDENTIFICADOR)
    public ScriptSenderRegistry execute() throws PackException {
        ScriptSenderRegistry registry = this.helper.findOrCreate(ScriptSenderRegistry.class, this.databaseID, this.mnemonico);
        registry.setMnemonico(this.mnemonico);
        registry.setDispatchScript(this.script);
        this.helper.save(registry, this.databaseID);
        return registry;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, ScriptSenderRegistry entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, IDENTIFICADOR, referenceMap.addReference(entity, "scriptSenderRegistry"));
        ExportUtils.put(bean, MNEMONICO, entity.getMnemonico());
        ExportUtils.put(bean, SCRIPT, entity.getDispatchScript().getMnemonico());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param mnemonico the mnemonico to set
     */
    @Input(fieldName = MNEMONICO)
    public void setMnemonico(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @param script the script to set
     */
    @Input(fieldName = SCRIPT)
    public void setScript(Script script) {
        this.script = script;
    }

    /**
     * @param databaseID the databaseID to set
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setDatabaseID(Long databaseID) {
        this.databaseID = databaseID;
    }
}
